import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
interface UserState {
  cookie: string
  isLogin: boolean
  useInfo: object
}

export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    useInfo: {},
    cookie: '',
    isLogin: false
  }),
  actions: {
    // 设置cookie
    setCookie(cookie: string) {
      this.cookie = cookie
    },
    // 设置用户信息
    setUserInfo(info: object) {
      this.useInfo = info
    },
    setIsLogin(isLogin: boolean) {
      this.isLogin = isLogin
    },
    logout() {
      this.$reset()
    },
    async setLoginStatus(proxy) {
      const res = await proxy.requestApi.login_status()
      if (res.data.code !== 200) {
        ElMessage.warning('获取登录状态失败')
        return
      }
      if (!res.data.account?.id) {
        ElMessage.warning('登录后可查看更多')
        this.setIsLogin(false)
        return
      }
      this.setIsLogin(true)
      this.setUserInfo(res.data.profile)
      console.log(res)
    }
  },
  getters: {}
})
